<template>
  <div class="setting-container">
    <!-- 卡片 -->
    <el-card>
      <!-- tabs -->
      <el-tabs v-model="activeName">
        <!-- 角色管理 -->
        <el-tab-pane label="角色管理" name="first">
          <template v-slot:label><span>角色管理</span></template>
          <!-- 按钮 -->
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="showDialog"
          >新增角色</el-button>
          <!-- table -->
          <el-table
            v-loading="loading"
            :data="tableData"
            border
            style="width:100%"
          >
            <el-table-column type="index" label="序号" width="180" />
            <el-table-column prop="name" sortable label="角色名" width="180" />
            <el-table-column prop="description" sortable label="描述" />
            <el-table-column width="300" label="操作">
              <template v-slot:default="{ row }">
                <el-button
                  type="text"
                  @click="showPerDialog(row.id)"
                >分配权限</el-button>
                <el-button
                  type="text"
                  @click="enterEdit(row.id)"
                >修改</el-button>
                <el-popconfirm
                  title="这是一段内容确定删除吗？"
                  @onConfirm="confirmHandler(row.id)"
                >
                  <el-button
                    slot="reference"
                    class="btn-del"
                    type="text"
                  >删除</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-pagination
            :current-page.sync="currentPage4"
            :page-size.sync="pageSize"
            layout="total, prev, pager, next"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </el-tab-pane>

        <!-- 公司信息 -->
        <el-tab-pane label="公司信息" name="second">
          <template v-slot:label><span>公司信息</span></template>
          <!-- form -->
          <settingForm />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 弹框 -->
    <settingDialog ref="dialog" />
    <!-- 分配权限的弹窗 -->
    <perDialog ref="perDialog" />
  </div>
</template>

<script>
import settingForm from './components/settingForm.vue'
import settingDialog from './components/settingDialog.vue'
import { getRoles, getRoleById, delRoleById } from '../../api/setting'
import perDialog from './components/perDialog.vue'
export default {
  name: 'Setting',
  components: {
    settingForm,
    settingDialog,
    perDialog
  },
  data() {
    return {
      activeName: 'first',
      tableData: [],
      currentPage4: 1,
      // 当前的页容量
      pageSize: 2,
      total: 21,
      loading: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    showPerDialog(id) {
      // console.log(id)
      this.$refs.perDialog.show(id)
    },
    async confirmHandler(id) {
      await delRoleById(id)
      this.$message.success('删除成功！')
      if (this.tableData.length === 1 && this.currentPage4 > 1) {
        this.currentPage4--
      }
      this.getData()
    },
    async enterEdit(id) {
      const res = await getRoleById(id)
      // console.log(res)
      this.$refs.dialog.form = res
      this.$refs.dialog.visible = true
    },
    showDialog() {
      this.$refs.dialog.visible = true
    },
    async getData() {
      this.loading = true
      const res = await getRoles({
        page: this.currentPage4,
        pagesize: this.pageSize
      })
      // console.log(res)
      this.tableData = res.rows
      this.total = res.total
      setTimeout(() => {
        this.loading = false
      }, 300)
    },

    // 页码改变
    // val 当前页码
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.getData()
    }
  }
}
</script>

<style lang="scss">
.setting-container {
  padding: 20px;
  background-color: #eef0f3;
  .el-tabs__item {
    font-size: 18px;
    span {
      padding: 0 20px;
    }
  }
  .el-table {
    margin-top: 10px;
    thead {
      th {
        background: #fafafa;
      }
    }
  }
  .el-pagination {
    text-align: right;
  }
  .btn-del {
    padding-left: 10px;
  }
}
</style>
